package org.akomantoso.core.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.akomantoso.core.data.group.ANblock;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.visitor.AKVisitor;

/**
 * The element item is a container belonging to a list.
 * 
 * <pre>
 * 	<xsd:element name="blockList">
 * 		<xsd:complexType>
 * 			<xsd:sequence>
 * 				<xsd:element ref="listIntroduction" minOccurs="0" maxOccurs="1"/>
 * 				<xsd:element ref="item" minOccurs="1" maxOccurs="unbounded"/>
 * 				<xsd:element ref="listConclusion" minOccurs="0" maxOccurs="1"/>
 * 			</xsd:sequence>
 * 			<xsd:attributeGroup ref="corereq"/>
 *		</xsd:complexType>
 * 	</xsd:element>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 */
public final class BlockList extends CoreReqImpl implements ANblock {

	public static final String ELEMENT = "blockList";
	
	private ListIntroduction _listIntroduction;
	
	private final List<Item> _items = new ArrayList<Item>(4);
	
	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <T extends Output> void writeExternal(T out) throws IOException {
		out.writeStart(ELEMENT);
		super.writeExternal(out);
		if (_listIntroduction != null) {
			_listIntroduction.writeExternal(out);
		}
		for (int i = 0, n = _items.size(); i < n; i++) {
			_items.get(i).writeExternal(out);
		}
		out.writeEnd();
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <T extends Input> void readExternal(T in) throws IOException {
		super.readExternal(in);
		in.readNextStartElement();
		if (ListIntroduction.ELEMENT == in.getLocalName()) {
			_listIntroduction = new ListIntroduction();
			_listIntroduction.readExternal(in);
			in.readNextStartOrEndElement();
		}
		
		Item item;
		while (Item.ELEMENT == in.getLocalName()) {
			_items.add(item = new Item());
			item.readExternal(in);
			in.readNextStartOrEndElement();
		}
		
	}

	/**
	 * @see org.akomantoso.core.visitor.AKVisitable#accept(org.akomantoso.core.visitor.AKVisitor)
	 */
	@Override
	public void accept(AKVisitor visitor) {
	}
	
}